package basic.clients;

import basic.Filter;
import basic.Notifiable;
import basic.Notification;
import basic.Op;
import basic.SienaException;
import basic.ThinClient;
import basic.comm.InvalidSenderException;


public class Publisher {

	/**
	 * @param args
	 * @throws InvalidSenderException 
	 */
	public static void main(String[] args) {
		
		ThinClient mySiena1 = null;
		ThinClient mySiena2 = null;
		
		//this is an SVN test.
		
		String msgs1 [] = {"ONE", "TWO", "THREE", "FOUR", "FIVE"};
		String msgs2 [] = {"one-", "two--", "three---", "four----", "five-----"};
		
		//Receivers of the messages
		Notifiable subscriber1 = new SimpleSubscriber("Sub 1");
		Notifiable subscriber2 = new SimpleSubscriber("Sub 2");
		
		//Two filters that will be used by publishers and subscribers
	    Filter f1 = new Filter();
	    Filter f2 = new Filter();
	    
	    //Add predicates to the filters
	    f1.addConstraint("message", "Hello, World!");
	    f2.addConstraint("message2", "Salaam!");
	    
	    
	    Notification n1 = new Notification();
	    Notification n2 = new Notification();
	    
	    n1.putAttribute("message","Hello, World!");
	    n2.putAttribute("message2","Salaam!");
	    
		try {
			mySiena1 = new ThinClient("udp:127.0.0.1:1111", "server1");
			mySiena2 = new ThinClient("udp:127.0.0.1:2222", "server2");
			
			mySiena1.advertise(f1, mySiena1.getServer());
		    mySiena2.advertise(f2, mySiena2.getServer());

		    mySiena1.subscribe(f2, subscriber1);
			mySiena2.subscribe(f1, subscriber2);
	
			
//			mySiena2.subscribe(f2, subscriber3);
			
			Thread.sleep(500);
			

			for(int i = 0; i < 5; i++){
				System.out.println("----------- published message number " + (i+1) + "-------------");
			//	n1.putAttribute("message", msgs1[i]);
			    mySiena1.publish(n1);
				mySiena2.publish(n2);
				Thread.sleep(1000);
			}
			
		} catch (InvalidSenderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SienaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		//	if(mySiena1 != null)
		//		mySiena1.shutdown();
		//	if(mySiena2 != null)
		//		mySiena2.shutdown();
			//System.exit(0);
		}
	}

}
